// ignore_for_file: file_names, slash_for_doc_comments, must_call_super, unnecessary_overrides, override_on_non_overriding_member, must_be_immutable, prefer_const_constructors, sized_box_for_whitespace, avoid_unnecessary_containers, avoid_print

import 'package:flutter/material.dart';
import 'package:mybase/base/abstract/IFFR.dart';
import 'package:mybase/base/abstract/IFFRefresh.dart';
import 'package:mybase/base/abstract/IFFScaffold.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';
import 'package:mybase/base/widget/page/FFScaffold.dart';
import 'package:mybase/base/tool/extension/FFListExt.dart';


class FFAnimatorBaseWidget extends StatefulWidget {
  String? name;
  Animation Function(AnimationController controller) createAnimation;
  Widget? Function(AnimationController controller,Animation animation) child;
  Duration? duration;
  bool? autoBegin;
  AnimationController? animationController; //外部不需要销毁，内部会处理

  FFAnimatorBaseWidget({
    Key? key,
    this.name,
    required this.child,
    required this.createAnimation,
    this.duration,
    this.autoBegin,
    this.animationController,
  }) : super(key: key);

  @override
  FFAnimatorBaseWidgetState createState() => FFAnimatorBaseWidgetState();
}

class FFAnimatorBaseWidgetState extends State<FFAnimatorBaseWidget> with TickerProviderStateMixin {
  late Animation animation;
  late AnimationController controller;

  @override
  void initState() {
    super.initState();

    controller = widget.animationController ?? AnimationController(duration: widget.duration ?? Duration(milliseconds: 240), vsync: this);

    // Animation<double> curve = CurvedAnimation(parent: controller, curve: Curves.ease);
    // animation = Tween(begin: 0.0, end: 1.0).animate(curve);
    // animation.addListener(() {
    //   setState(() => {});
    // });

    animation = widget.createAnimation(controller);
    animation.addListener(() {
      if (mounted) {
        setState(() {});
      }
    });

    if (widget.autoBegin == true) {
      controller.forward();
    }
  }

  @override
  void dispose() {
    //在super.dispose()之前销毁才行
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child(controller,animation)??Container();
  }


  //用法举例，可复制
  Widget useDemo(){
    //late AnimationController animatedContainer;
    // init
    // animatedContainer = AnimationController(duration: Duration(milliseconds: 220), vsync: this);
    return FFAnimatorBaseWidget(
      // animationController: animatedContainer,
      createAnimation: (controller) {
        Animation<double> curve = CurvedAnimation(parent: controller, curve: Curves.ease);
        return Tween(begin: 0.0, end: 1.0).animate(curve);
      },
      child: (controller, animation) {
        return Container(
          width: 100,
          child: FFLineContainer(
            children: [
              FFTextButton(
                padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                unconstrainedBox: true,
                text: "动画放大效果",
                color: Colors.pink,
                onClick: () {
                  controller.forward();
                },
              ),
              FFTextButton(
                padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                unconstrainedBox: true,
                text: "动画缩小效果",
                color: Colors.pink,
                onClick: () {
                  controller.reverse();
                },
              ),
              Container(
                width: 100,
                height: 100.0 * animation.value,
                color: Colors.blue,
                child: CustomScrollView(
                  slivers: [
                    SliverList(
                      delegate: SliverChildBuilderDelegate(
                            (BuildContext ctx, int index) {
                          return Container(
                            height: 50,
                            margin: EdgeInsets.only(top: 1),
                            color: Colors.red,
                          );
                        },
                        childCount: 2,
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}


